package com.lianniu.web.product.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.constants.ErrorCode;
import com.lianniu.web.pagination.PaginationInfo;
import com.lianniu.web.product.dao.IProductSpecDao;
import com.lianniu.web.product.model.ProductSpecVo;
import com.lianniu.web.product.service.IProductSpecService;
import com.lianniu.web.util.StringUtil;

@Service
public class ProductSpecServiceImpl implements IProductSpecService {
	/**
	 * 
	*/
	private final static Logger logger = Logger.getLogger(ProductSpecServiceImpl.class);
	
	@Resource
	IProductSpecDao productSpecDao;

	@Override
	public BaseResult selectByProductSpecForPage(ProductSpecVo psVo,
			PaginationInfo paginationInfo) {
		logger.info("selectByProductSpecForPage begin.");
	    if (!StringUtil.isEmpty(psVo.getSpecName())) {
	    	psVo.setSpecName(psVo.getSpecName().replace(" ", ""));
	    }
	    logger.info("selectByProductSpecForPage specName:" +psVo.getSpecName());
	    if (!StringUtil.isEmpty(psVo.getSpecId())) {
	    	psVo.setSpecId(psVo.getCateId().replace(" ", ""));
	    }
	    logger.info("selectByProductSpecForPage SpecId:" +psVo.getSpecId());
	    logger.info("selectByProductSpecForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    List<ProductSpecVo> list=productSpecDao.selectByProductSpecForPage(psVo);
	    Map<String,Object> map=new HashMap<String,Object>();
	    map.put("total", paginationInfo.getTotalRecord());
	    map.put("curPage", paginationInfo.getCurPage());
	    map.put("rows", list);
	    logger.info("selectByProductSpecForPage:  end");    
	    
	    return BaseResult.success(map); 
	}

	@Override
	public BaseResult selectById(ProductSpecVo psVo) {
		 logger.info("selectById begin");
		    logger.info("selectById Id:" +psVo.getSpecId());
		  
		    return BaseResult.success(productSpecDao.selectById(psVo.getSpecId()));
	}

	@Override
	public BaseResult selectParentSpec(String cateId) {
		logger.info("selectParentSpec begin");
	    logger.info("selectParentSpec parm: null");
	  
	    return BaseResult.success(productSpecDao.getProductCateKeyValue(cateId));
	}

	@Override
	public BaseResult selectByCateId(String cateId) {
		logger.info("selectByCateId begin");
		logger.info("selectByCateId cateId="+cateId);
		return BaseResult.success(productSpecDao.selectByCateId(cateId));
	}
	@Override
	public BaseResult selectByProductCateId(String cateId) {
		logger.info("selectByProductCateId begin");
		logger.info("selectByProductCateId cateId:"+cateId);
		return BaseResult.success(productSpecDao.selectByProductCateId(cateId));
	}


}
